/**
 * updater for ducks
 */
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Class that updates a JPanel and causes it to repaint after interval
 * of a specified seconds.
 * @author Kevin Hampton
 * @version 1.0
 */

public class DuckUpdater implements ActionListener {
	/**
	 * Field YIBAI.
	 * (value is 100)
	 */
	private static final int YIBAI = 100;

	/**
	 * Field YIQIAN.
	 * (value is 1000)
	 */
	private static final int YIQIAN = 1000;

	/**
	 * Method toString.
	
	 * @return String */
	@Override
	public String toString() {
		return "DuckUpdater [panelToUpdate=" + panelToUpdate + ", timer="
				+ timer + "]";
	}

	/**
	 * Field panelToUpdate.
	 */
	private final DuckHuntPanel panelToUpdate;
	
	/**
	 * Field timer.
	 */
	public Timer timer;
	
	/**
	 * Constructor for PanelUpdater. Takes in the panel it calls repaint on
	 * and the time interval in seconds that it operates on.
	 * @param panelToUpdate
	 * @param secondsInterval
	
	 */
	public DuckUpdater(DuckHuntPanel panelToUpdate, float secondsInterval) {
		this.panelToUpdate = panelToUpdate;
		//Converts float seconds interval given into milliseconds 
		final int interval = (int) (secondsInterval * YIQIAN);
		timer = new Timer(interval, this);
	}
	
	/**
	 * Starts the timer
	 */
	public void start() {
		timer.start();
	}
	
	/**
	 * Stops the timer
	 */
	public void stop() {
		timer.stop();
	}
	
	/**
	 * Method setEndGame.
	 * @param temp int
	 */
	public void setEndGame(int temp) {
		timer.setDelay(YIBAI);
	}
	
	/**
	 * Updates the panel everytime the timer ticks
	 * @param event ActionEvent
	
	 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
	@Override
	public void actionPerformed(ActionEvent event) {
		this.panelToUpdate.updateTargets();
		this.panelToUpdate.repaint();

	}
}
